<extend name="Application/Admin/View/Layout/application.html"/>

<block name="css">
    <style>
        #page2 {
            display: none;
        }
    </style>
</block>


<block name="content">
    <div class="row">
        <div class="col-md-12">

            <div class="row">
                <div class="col-md-8">
                    <div class="page-header">
                        <h1>友情链接</h1>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-10">
                    <div class="panel panel-default">
                        <div class="panel-heading">Link Config</div>

                        <div class="panel-body">
                            <form action="" method="post" id="form">
                            <table class="table table-hover">
                                <thead>
                                <tr>
                                    <th><input type="checkbox" id="check_all"></th>
                                    <th>链接名称</th>
                                    <th>网址</th>
                                    <th>排序</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>


                                <volist name="links" id="link">
                                    <tr>
                                        <th scope="row">
                                            <input type="checkbox" class="delete_checked" name="delete_ids[]" value="{{$link.id}}"></th>
                                        <td class="name">{{$link.name}}</td>
                                        <td class="url">{{$link.url}}</td>
                                        <td>
                                            <input type="hidden" name="ids[]" value="{{$link.id}}">
                                            <input type="text"  name="sort_order[]" value="{{$link.sort_order}}">
                                        </td>
                                        <td>
                                            <a href="" data-toggle="modal" data-target="#edit_link"
                                               data-id="{{$link.id}}" class="edit">
                                                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                                            </a>
                                                <a href="{{:U('destroy',array('id'=>$link['id']))}}">
                                                <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                                            </a>
                                        </td>
                                    </tr>
                                </volist>


                                </tbody>
                            </table>
                            </form>


                        </div>
                    </div>
                </div>

                <div class="col-md-2">
                    <div class="list-group">
                        <a href="javascript: void 0;" class="list-group-item list-group-item-info create_link"
                           data-toggle="modal" data-target="#create_link">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 新增 (add)
                        </a>

                        <a href="javascript: void 0;" class="list-group-item list-group-item-info destroy_checked">
                            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除 (destroy)
                        </a>

                        <a href="javascript: void 0;" class="list-group-item list-group-item-info sort_link">
                            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> 排序 (sort)
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- 新增Modal -->
    <div class="modal fade" id="create_link" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <form class="form-horizontal" action="{{:U('store')}}" method="post">

                    <div class="modal-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">链接名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="inputEmail3" name="name" placeholder="name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">网址</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="url" id="url" >
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">新 增</button>
                    </div>
                </form>

            </div>
        </div>
    </div>


    <!-- 编辑Modal -->
    <div class="modal fade" id="edit_link" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="">编辑链接</h4>
                </div>
                <form class="form-horizontal" action="{{:U('update')}}" method="post">

                    <input type="hidden" name="id" id="edit_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">链接名称</label>
                            <div class="col-sm-10">
                                <input type="text" value="{{$link.name}}" class="form-control" name="name" id="edit_name" placeholder="name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">网址</label>
                            <div class="col-sm-10">
                                <input type="text" value="{{$link.url}}"  class="form-control" name="url" id="edit_url" placeholder="url">
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">修 改</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
</block>



<block name="js">
    <script>
        $(function () {
            $(".edit").click(function () {
                var $tr = $(this).parents("tr");
                var name = $tr.children(".name").text();
                var url = $tr.children(".url").text();
                var id = $(this).data("id");

                $("#edit_name").val(name);
                $("#edit_url").val(url);
                $("#edit_id").val(id);
            })


            $("#check_all").click(function () {
                //所有checkbox跟着全选的checkbox走。
                $('.delete_checked').prop("checked", this.checked);
            });
            $(".destroy_checked").click(function() {
                var len = $(".delete_checked:checked").length;
                if (len == 0) {
                    alert("您必须至少选择一条");
                    return false;
                }
                $("#form").attr("action", "{{:U('destroy_checked')}}").submit();
            })
            $(".sort_link").click(function(){
                $("#form").attr("action","{{:U('sort_link')}}").submit();
            })
        })

    </script>

</block>
